package com.avic.rong.userOrder.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.activity.product.DetailProductActivity;
import com.avic.rong.userOrder.bean.OrderDetailsBean;
import com.avic.rong.utils.GlideUtils;
import com.bumptech.glide.Glide;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.List;

/**
 * Created by XinEr on 2019/7/29.
 * Describe:
 */
public class OrderGoodsDetailsItemAdapter extends RecyclerView.Adapter<OrderGoodsDetailsItemAdapter.ViewHolder> {
    private Context context;
    private List<OrderDetailsBean.DeliveryListBean.ItemsBean> items;
    private OrderDetailsBean orderDetailsBean;



    public OrderGoodsDetailsItemAdapter(Context context, List<OrderDetailsBean.DeliveryListBean.ItemsBean> items, OrderDetailsBean orderDetailsBean) {
        this.context = context;
        this.items = items;
        this.orderDetailsBean = orderDetailsBean;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.order_goods_details_item, viewGroup, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        OrderDetailsBean.DeliveryListBean.ItemsBean itemsBeanXX = items.get(i);

//        GlideUtils.loaderImage(context,itemsBeanXX.getGoodsPrice()+"",viewHolder.iv_photo);
        if (itemsBeanXX.getGood() == null) return;
        OrderDetailsBean.DeliveryListBean.ItemsBean.GoodBean good = itemsBeanXX.getGood();
        viewHolder.tv_title.setText(good.getGoodsName());
//        viewHolder.tv_time.setText(itemsBeanXX.getSkuName()+"");
        viewHolder.tv_price.setText("¥" + good.getShopPrice());
        viewHolder.tv_order_number.setText("x" + itemsBeanXX.getGoodsNumber());
        viewHolder.tv_status.setText(good.getAttrShow());
        String goodsImg = itemsBeanXX.getGood().getGoodsImg();

        if (!TextUtils.isEmpty(itemsBeanXX.getGood().getAttrShow())){
            viewHolder.tv_status.setText(itemsBeanXX.getGood().getAttrShow());
            viewHolder.tv_status.setVisibility(View.VISIBLE);
        }


        JSONArray jsonArray = null;
        String skuName = "";
        try {
            if (TextUtils.isEmpty(itemsBeanXX.getGood().getSkuName())) {
                return;
            }
            jsonArray = new JSONArray(itemsBeanXX.getGood().getSkuName());
            StringBuffer paramsSb = new StringBuffer("");
            StringBuffer moreParamsb = new StringBuffer("");
            for (i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                Iterator<String> keys = jsonObject.keys();
                String key = "";
                while (keys.hasNext()) {//遍历JSONObject

                    key = (String) keys.next().toString();

                    if (null != key && !"".equals(key)) {

                        String value = (String) jsonObject.get(key);
                        if (jsonArray.length()>1){
//                            skuName = value +"; ";
                            paramsSb.append(value + " ; ");
                        }else {
                            paramsSb.append(value);
                        }

                    }

                }


            }
            if (jsonArray.length()>1){
                moreParamsb.append(paramsSb.deleteCharAt(paramsSb.length() - 2));
                viewHolder.tv_time.setText(moreParamsb);
            }else {
                viewHolder.tv_time.setText(paramsSb.toString());
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }




        if (orderDetailsBean != null) {
           /* if (orderDetailsBean.getOrder().getOrderType()==4){
                if (!TextUtils.isEmpty(goodsImg)){
                    if (goodsImg.contains("rongrong.")){
                        GlideUtils.loaderImage(context, goodsImg,  viewHolder.iv_photo);
                    }else {
                        String biaoZhunImg = orderDetailsBean.getBiaoZhunImg();
                        GlideUtils.loaderImage(context,biaoZhunImg+goodsImg,  viewHolder.iv_photo);
                    }
                    viewHolder.iv_photo.setVisibility(View.VISIBLE);
                    viewHolder.tv_goods_pic.setVisibility(View.GONE);
                }else {
                    String keywords = itemsBeanXX.getProModel();
                    viewHolder.tv_goods_pic.setText(keywords);
                    viewHolder.tv_goods_pic.setVisibility(View.VISIBLE);
                    viewHolder.iv_photo.setVisibility(View.GONE);
                }
            }else {
                if (!TextUtils.isEmpty(goodsImg)){
                    GlideUtils.loaderImage(context, goodsImg,  viewHolder.iv_photo);
                }else {
                    //默认图
                    GlideUtils.loaderImage(context, R.mipmap,  viewHolder.iv_photo);
                }
                viewHolder.iv_photo.setVisibility(View.VISIBLE);
                viewHolder.tv_goods_pic.setVisibility(View.GONE);
            }*/


            if (orderDetailsBean.getOrder().getOrderType() == 4) {
                if (!TextUtils.isEmpty(goodsImg)) {
                    if (goodsImg.contains("rongrong.")) {
                        GlideUtils.loaderImage(context, goodsImg, viewHolder.iv_photo);
                    } else {
                        String biaoZhunImg = orderDetailsBean.getBiaoZhunImg();
                        GlideUtils.loaderImage(context, biaoZhunImg + goodsImg, viewHolder.iv_photo);
                    }
                    viewHolder.iv_photo.setVisibility(View.VISIBLE);
                    viewHolder.ll_pic_tag.setVisibility(View.GONE);
                } else {
                    String keywords = itemsBeanXX.getProModel();
                    String catName = itemsBeanXX.getGood().getCatName();
                    viewHolder.tv_goods_type.setText(catName);
                    viewHolder.tv_goods_tel.setText(keywords);
                    viewHolder.ll_pic_tag.setVisibility(View.VISIBLE);
                    viewHolder.iv_photo.setVisibility(View.VISIBLE);
                    Glide.with(context).load(R.mipmap.common_bg).into(viewHolder.iv_photo);
                }
            } else {
                if (!TextUtils.isEmpty(goodsImg)) {
                    GlideUtils.loaderImage(context, goodsImg, viewHolder.iv_photo);
                    viewHolder.iv_photo.setVisibility(View.VISIBLE);
                    viewHolder.ll_pic_tag.setVisibility(View.GONE);
                } else {
                    //默认图
//                GlideUtils.loaderImage(context, R.mipmap.common_bg, iv_goods_pic);
                    String keywords = itemsBeanXX.getProModel();
                    String catName = itemsBeanXX.getGood().getCatName();
                    viewHolder.tv_goods_type.setText(catName);
                    viewHolder.tv_goods_tel.setText(keywords);
                    Glide.with(context).load(R.mipmap.common_bg).into(viewHolder.iv_photo);
                    viewHolder.iv_photo.setVisibility(View.VISIBLE);
                    viewHolder.ll_pic_tag.setVisibility(View.VISIBLE);
                }

            }

        }
        viewHolder.ll_root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(itemsBeanXX.getSkuId()+""))return;
                context.startActivity(DetailProductActivity.getIntent(context,itemsBeanXX.getSkuId()+""));
            }
        });
     /*   if (items.size()-1==i){
            viewHolder.line_view.setVisibility(View.GONE);
        }*/


    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_photo;
        private TextView tv_title;
        private TextView tv_time;
        private TextView tv_price;
        private TextView tv_order_number;
        private TextView tv_status;
        private TextView tv_goods_type;
        private TextView tv_goods_tel;
        private LinearLayout ll_pic_tag;
        private LinearLayout ll_root;
        private View line_view;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_photo = itemView.findViewById(R.id.iv_photo);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_price = itemView.findViewById(R.id.tv_price);
            tv_order_number = itemView.findViewById(R.id.tv_order_number);
            tv_status = itemView.findViewById(R.id.tv_status);
            tv_goods_type = itemView.findViewById(R.id.tv_goods_type);
            tv_goods_tel = itemView.findViewById(R.id.tv_goods_tel);
            ll_pic_tag = itemView.findViewById(R.id.ll_pic_tag);
            ll_root = itemView.findViewById(R.id.ll_root);
            line_view = itemView.findViewById(R.id.line_view);
        }
    }
}
